<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha256-CTSx/A06dm1B063156EVh15m6Y67pAjZZaQc89LLSrU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.css" integrity="sha256-vQkngPS8jiHHH0I6ABTZroZk8NPZ7b+MUReOFE9UsXQ=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"rjyblog.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.18.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":null},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="1234567891011121314151617181920212223242526272829303132333435class Base &amp;#123;public:    int baseValue;    virtual void baseFun() &amp;#123;&amp;#125;&amp;#125;;class B &amp;#123;public:    int bValue;    virtual voi">
<meta property="og:type" content="article">
<meta property="og:title" content="C++类内存布局">
<meta property="og:url" content="https://rjyblog.gitee.io/posts/77080af6.html">
<meta property="og:site_name" content="任建勇的博客">
<meta property="og:description" content="1234567891011121314151617181920212223242526272829303132333435class Base &amp;#123;public:    int baseValue;    virtual void baseFun() &amp;#123;&amp;#125;&amp;#125;;class B &amp;#123;public:    int bValue;    virtual voi">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://rjyblog.gitee.io/images/C++%E5%AF%B9%E8%B1%A1%E5%86%85%E5%AD%98%E5%B8%83%E5%B1%80.drawio.svg">
<meta property="article:published_time" content="2024-01-12T14:12:12.000Z">
<meta property="article:modified_time" content="2024-01-13T08:24:25.401Z">
<meta property="article:author" content="Jason">
<meta property="article:tag" content="C++">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://rjyblog.gitee.io/images/C++%E5%AF%B9%E8%B1%A1%E5%86%85%E5%AD%98%E5%B8%83%E5%B1%80.drawio.svg">


<link rel="canonical" href="https://rjyblog.gitee.io/posts/77080af6.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://rjyblog.gitee.io/posts/77080af6.html","path":"posts/77080af6.html","title":"C++类内存布局"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>C++类内存布局 | 任建勇的博客</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">任建勇的博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E7%B1%BB%E5%86%85%E5%AD%98%E5%B8%83%E5%B1%80%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">查看类内存布局的方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8VisualStudio"><span class="nav-text">使用VisualStudio</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8clang"><span class="nav-text">使用clang</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-text">参考</span></a></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Jason"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">Jason</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">66</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://rjyblog.gitee.io/posts/77080af6.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="Jason">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="任建勇的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="C++类内存布局 | 任建勇的博客">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          C++类内存布局
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-01-12 22:12:12" itemprop="dateCreated datePublished" datetime="2024-01-12T22:12:12+08:00">2024-01-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-01-13 16:24:25" itemprop="dateModified" datetime="2024-01-13T16:24:25+08:00">2024-01-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/C-C/" itemprop="url" rel="index"><span itemprop="name">C/C++</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="type">int</span> baseValue;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">baseFun</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="type">int</span> bValue;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">fb1</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">fb2</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123; cout &lt;&lt; <span class="string">&quot;B::test()&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">C</span> : <span class="keyword">public</span> Base &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="type">int</span> cValue;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">fc1</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">fc2</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123; cout &lt;&lt; <span class="string">&quot;C::test()&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> : <span class="keyword">public</span> B, <span class="keyword">public</span> C</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="type">int</span> aPublicValue;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">fb2</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="comment">//virtual void test() &#123; cout &lt;&lt; &quot;A::test()&quot; &lt;&lt; endl; &#125;</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">fc2</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">funcA1</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">funcA2</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">funcA3</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">short</span> aPrivateValue;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><img src="/images/C++%E5%AF%B9%E8%B1%A1%E5%86%85%E5%AD%98%E5%B8%83%E5%B1%80.drawio.svg" alt="C++对象内存布局"></p>
<p>通过VisualStudio来查看A类的内存布局：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">class A	size(28):</span><br><span class="line">	+---</span><br><span class="line"> 0	| +--- (base class B)</span><br><span class="line"> 0	| | &#123;vfptr&#125;</span><br><span class="line"> 4	| | bValue</span><br><span class="line">	| +---</span><br><span class="line"> 8	| +--- (base class C)</span><br><span class="line"> 8	| | +--- (base class Base)</span><br><span class="line"> 8	| | | &#123;vfptr&#125;</span><br><span class="line">12	| | | baseValue</span><br><span class="line">	| | +---</span><br><span class="line">16	| | cValue</span><br><span class="line">	| +---</span><br><span class="line">20	| aPublicValue</span><br><span class="line">24	| aPrivateValue</span><br><span class="line">  	| &lt;alignment member&gt; (size=2)</span><br><span class="line">	+---</span><br><span class="line"></span><br><span class="line">A::$vftable@B@:</span><br><span class="line">	| &amp;A_meta</span><br><span class="line">	|  0</span><br><span class="line"> 0	| &amp;B::fb1 </span><br><span class="line"> 1	| &amp;A::fb2 </span><br><span class="line"> 2	| &amp;B::test </span><br><span class="line"> 3	| &amp;A::funcA1 </span><br><span class="line"> 4	| &amp;A::funcA2 </span><br><span class="line"></span><br><span class="line">A::$vftable@C@:</span><br><span class="line">	| -8</span><br><span class="line"> 0	| &amp;Base::baseFun </span><br><span class="line"> 1	| &amp;C::fc1 </span><br><span class="line"> 2	| &amp;A::fc2 </span><br><span class="line"> 3	| &amp;C::test </span><br><span class="line"></span><br><span class="line">A::fb2 this adjustor: 0</span><br><span class="line">A::fc2 this adjustor: 8</span><br><span class="line">A::funcA1 this adjustor: 0</span><br><span class="line">A::funcA2 this adjustor: 0</span><br></pre></td></tr></table></figure>

<p>从<code>A::$vftable@B@</code>可以看出虚函数表中A和B合并在一起了，并且<code>A::fb2</code>覆盖了<code>B::fb2</code>。而C类的vftable则是独立存储。</p>
<p>B和C两个类都定义了test()函数，而A类则没有覆盖test方法，如果A类调用test方法到底采用的是那个呢？在<code>A::$vftable@B@</code>指向的是<code>B::test</code>。而<code>A::$vftable@C@</code>指向的是<code>C::test</code>。我们通过下面代码进行测试：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    A a;</span><br><span class="line">    <span class="comment">//a.test(); //此处会导致编译错误，因为无法确定test的实现</span></span><br><span class="line">    a.B::<span class="built_in">test</span>(); <span class="comment">//通过限定引用可以解决多义性问题</span></span><br><span class="line">    B&amp; b = <span class="built_in">static_cast</span>&lt;B&amp;&gt;(a);</span><br><span class="line">    b.<span class="built_in">test</span>();  <span class="comment">//打印 B::test()</span></span><br><span class="line">    C* c = <span class="built_in">static_cast</span>&lt;C*&gt;(&amp;a);</span><br><span class="line">    c-&gt;<span class="built_in">test</span>(); <span class="comment">//打印 C::test()</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>所以基于A类的实例是无法调用test函数的，但是把A类转换成B/C的引用或者指针后，就能调用test方法了，但只能分别调用自己的函数实现。</p>
<p>我们现在把C继承Base使用virtual进行修饰，代码改动如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">C</span> : <span class="keyword">public</span> <span class="keyword">virtual</span> Base</span><br></pre></td></tr></table></figure>

<p>A类的内存布局情况如下：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">class A	size(36):</span><br><span class="line">	+---</span><br><span class="line"> 0	| +--- (base class B)</span><br><span class="line"> 0	| | &#123;vfptr&#125;</span><br><span class="line"> 4	| | bValue</span><br><span class="line">	| +---</span><br><span class="line"> 8	| +--- (base class C)</span><br><span class="line"> 8	| | &#123;vfptr&#125;</span><br><span class="line">12	| | &#123;vbptr&#125;</span><br><span class="line">16	| | cValue</span><br><span class="line">	| +---</span><br><span class="line">20	| aPublicValue</span><br><span class="line">24	| aPrivateValue</span><br><span class="line">  	| &lt;alignment member&gt; (size=2)</span><br><span class="line">	+---</span><br><span class="line">	+--- (virtual base Base)</span><br><span class="line">28	| &#123;vfptr&#125;</span><br><span class="line">32	| baseValue</span><br><span class="line">	+---</span><br><span class="line"></span><br><span class="line">A::$vftable@:</span><br><span class="line">	| &amp;A_meta</span><br><span class="line">	|  0</span><br><span class="line"> 0	| &amp;B::fb1 </span><br><span class="line"> 1	| &amp;A::fb2 </span><br><span class="line"> 2	| &amp;B::test </span><br><span class="line"> 3	| &amp;A::funcA1 </span><br><span class="line"> 4	| &amp;A::funcA2 </span><br><span class="line"></span><br><span class="line">A::$vftable@C@:</span><br><span class="line">	| -8</span><br><span class="line"> 0	| &amp;C::fc1 </span><br><span class="line"> 1	| &amp;A::fc2 </span><br><span class="line"> 2	| &amp;C::test </span><br><span class="line"></span><br><span class="line">A::$vbtable@:</span><br><span class="line"> 0	| -4</span><br><span class="line"> 1	| 16 (Ad(C+4)Base)</span><br><span class="line"></span><br><span class="line">A::$vftable@Base@:</span><br><span class="line">	| -28</span><br><span class="line"> 0	| &amp;Base::baseFun </span><br><span class="line"></span><br><span class="line">A::fb2 this adjustor: 0</span><br><span class="line">A::fc2 this adjustor: 8</span><br><span class="line">A::funcA1 this adjustor: 0</span><br><span class="line">A::funcA2 this adjustor: 0</span><br><span class="line">vbi:	   class  offset o.vbptr  o.vbte fVtorDisp</span><br><span class="line">            Base      28      12       4 0</span><br></pre></td></tr></table></figure>

<p>可以看出class c中多了一个{vbptr}指针，这个叫做虚基类指针。</p>
<h3 id="查看类内存布局的方法"><a href="#查看类内存布局的方法" class="headerlink" title="查看类内存布局的方法"></a>查看类内存布局的方法</h3><h4 id="使用VisualStudio"><a href="#使用VisualStudio" class="headerlink" title="使用VisualStudio"></a>使用VisualStudio</h4><p>在VisualStudio中：工具 &gt; 命令行 &gt; 开发者命令行提示，然后输入命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cl /d1 reportAllClassLayout xxx.cpp &gt; class.txt</span><br></pre></td></tr></table></figure>

<p>输入内容比较多，搜索“class ClassName”就可以找到对应类的内部布局信息。</p>
<h4 id="使用clang"><a href="#使用clang" class="headerlink" title="使用clang"></a>使用clang</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">clang -Xclang -fdump-record-layouts -stdlib=libc++  -std=gnu++11 xxx.cpp</span><br><span class="line">clang -Xclang -fdump-vtable-layouts -stdlib=libc++ -std=gnu++11 -I/usr/include/c++/11 -I/usr/include/x86_64-linux-gnu/c++/11 -c xxx.cpp</span><br></pre></td></tr></table></figure>

<p>这种方式好像在继承关系使用virtual修饰时没有</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">*** Dumping AST Record Layout</span><br><span class="line">         0 | class A</span><br><span class="line">         0 |   class B (primary base)</span><br><span class="line">         0 |     (B vtable pointer)</span><br><span class="line">         8 |     int bValue</span><br><span class="line">        16 |   class C (base)</span><br><span class="line">        16 |     (C vtable pointer)</span><br><span class="line">        24 |     int cValue</span><br><span class="line">        28 |   int aPublicValue</span><br><span class="line">        32 |   short aPrivateValue</span><br><span class="line">        36 |   class Base (virtual base)</span><br><span class="line">        36 |     int baseValue</span><br><span class="line">           | [sizeof=40, dsize=40, align=8,</span><br><span class="line">           |  nvsize=34, nvalign=8]</span><br></pre></td></tr></table></figure>

<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://learn.microsoft.com/zh-cn/cpp/cpp/multiple-base-classes?view=msvc-170">多个基类</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/C/" rel="tag"># C++</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/9b085480.html" rel="prev" title="Android性能优化">
                  <i class="fa fa-angle-left"></i> Android性能优化
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/99c27b28.html" rel="next" title="C++智能指针">
                  C++智能指针 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Jason</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener external nofollow noreferrer" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener external nofollow noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.umd.js" integrity="sha256-oyhjPiYRWGXaAt+ny/mTMWOnN1GBoZDUQnzzgC7FRI4=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>


  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.5.0/mermaid.min.js","integrity":"sha256-K7oJiQlDulzl24ZUFOywuYme1JqBBvQzK6m8qHjt9Gk="}}</script>
  <script type="module" src="/js/zenuml-definition-074a43fa.js"></script>
  <script type="module" src="/js/mermaid-zenuml.esm.min.mjs"></script>
  <script src="/js/third-party/tags/mermaid.js"></script>


  <script src="/js/third-party/fancybox.js"></script>



  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
